/*
 * Golazo MA4
 * Copyright (C) 2008 Golazo MASHUP AWARD 4th team.
 * http://code.google.com/p/golazo/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * authUser
 */ 
function authUser(userId, password) {

  // 会員IDが期待どおりか
  if (!Golazo.User.checkUserId(userId))
    return false;

  // パスワードが期待どおりか
  if (!Golazo.User.checkPassword(password))
    return false;

  // 会員を認証する
  return !!Golazo.User.auth(userId, password);
};
//authUser.proxy = true;

/**
 * checkUrl
 */
function checkUrl() {
  
  var url = $('#form_url')
    .val();
  
  return url === '' || url.substr(0, 1) === '/'; 
}

/**
 * checkUserIdPassword
 */
function checkUserIdPassword() {

  var userId = $('#form_userId');
  var password = $('#form_password');

  var message = $('#form_message')
    .hide(); 

  if (!authUser(userId.val(), password.val())) {

	message
	  .text('会員 ID またはパスワードに誤りがあります。正しい会員 ID とパスワードを入力してください。')
	  .show();
	
	return false;
  }

  return true;
}
//checkUserIdPassword.runat = 'both';

/**
 * serverload
 */
function serverload() {

  // dispatch 
  Golazo.dispatch(function(request, response, session) {

    // 私は誰か
    var me = Golazo.User.find(session.get('me'));
	if (me)
	  return $('#succeeded')
	    .layout(me)
        .metaRefresh('/me.html', 3);

    // GET
    if (request.method != 'POST') {

      var url = $('#form_url')
        .val(request.data.url || '/me.html');

	  return $('#form')
	    .layout(/*me*/);
	
	// POST
	} else {
  
      var url = $('#form_url')
        .val(request.data.url || '');

      var userId = $('#form_userId')
        .val(request.data.userId || '');

      var password = $('#form_password')
        .val(request.data.password || '');

      // 遷移先URLが期待どおりか
      if (!checkUrl())
	    url.val('/me.html');

      // 会員ID、パスワードが期待どおりか
	  if (!checkUserIdPassword())
        return $('#form')
		  .layout(/*me*/);
      
	  // 会員を検索する
	  me = Golazo.User.find(userId.val());
	  if (!me)
	    throw new Error('(me)');

      // アクセス日時を保存する
	  try {
		me.lastLogin = new Date();
	    me.save();
	  } catch (e) {
	    throw new Error(''+e);
	  }
  
      session.set('me', me.userId);
  
      return $('#succeeded')
	    .layout(me)
        .metaRefresh(url.val(), 3);
	}
  });
};

/**
 * load
 */
function load() {
  $(document.form).submit(function() {
    //return checkUserIdPassword();
    return true;
  });
};
load.runat = 'client';